package com.caseProcess.controller;

import com.caseProcess.entity.*;
import com.caseProcess.service.DepartmentService;
import com.caseProcess.service.RoleService;
import com.caseProcess.utils.IdWorker;
import com.caseProcess.utils.PagingUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ahuo
 * @version 1.0
 * @date 2020年11月15日 13点15分
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;
    IdWorker idWorker = new IdWorker(1, 1, 1);

    /*查看角色列表信息*/
    @RequestMapping("/select")
    public Result selectRole(@RequestParam(required = false, defaultValue = "") String roleName, @RequestParam(required = false, defaultValue = "0") int page, @RequestParam(required = false, defaultValue = "0") int limit) {
        Result result = new Result();
        List<Role> rolelist = new ArrayList<Role>();
        Role role = new Role();
        role.setRoleName(roleName);
        rolelist = roleService.selectRole(role);
        result.setCount(rolelist.size());
        if (rolelist.size() >= 0) {
            if(page!=0&&limit!=0){
                rolelist= PagingUtil.paging(rolelist,page,limit);
            }
            result.setCode(Result.RESULT_OK);
            result.setMsg("角色信息查询成功");
            result.setData(rolelist);
            return result;
        } else {
            result.setCount(0);
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("服务器错误");
            return result;
        }

    }

    /*添加角色信息*/
    @CrossOrigin
    @RequestMapping("/insert")
    public Result insertRole(String roleName, Long grade, String description) {
        Result result = new Result();
        Role role = roleService.isExist(roleName);
        if (role != null) {
            result.setCode(result.RESULT_ERROR);
            result.setMsg("角色信息添加失败");
            return result;
        } else {
            role = new Role();
            role.setRoleName(roleName);
            role.setDescription(description);
            role.setDeepth(1);
            role.setParentId(0);
            role.setGrade(grade);
            roleService.insert(role);
            result.setCode(Result.RESULT_OK);
            result.setMsg("角色信息添加成功");
            result.setData(role);
            return result;
        }
    }

    /*修改角色信息*/
    @CrossOrigin
    @RequestMapping("update")
    public Result updateRole(Long roleId, String roleName, String description, Long grade) {
        Role role = new Role();
        role.setRoleId(roleId);
        role.setRoleName(roleName);
        role.setDescription(description);
        role.setGrade(grade);

        roleService.update(role);
        Result result = new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("角色信息修改成功");
        result.setData(role);
        return result;
    }

    /*删除角色信息*/
    @CrossOrigin
    @RequestMapping("delete")
    public Result deleteRole(int roleId) {
        Role role = new Role();
        role.setRoleId(roleId);
        int a = roleService.delete(role);
        Result result = new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("角色信息删除成功");
        result.setData(a);
        return result;
    }
}
